{include file='common/_meta' /}
<body>
<div class="container">

	<form class="layui-form" action="" lay-filter="form-filter" id="form-article">

		<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label"><span class="custom-color-red">*</span>用户登录名：</label>
		    	<div class="layui-input-block">
			     	<input type="text" name="admin_name" autocomplete="off" class="layui-input" lay-verify="required|admin_name">
		    	</div>
		 	</div>
	 	</div>
		<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label">(业务员)姓名：</label>
		    	<div class="layui-input-block">
			     	<input type="text" name="name" autocomplete="off" class="layui-input" >
		    	</div>
		 	</div>
	 	</div>

	 	<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label"><span class="custom-color-red">*</span>初始密码：</label>
		    	<div class="layui-input-block">
			     	<input type="password" name="password" id="password" autocomplete="off" class="layui-input" lay-verify="required|password">
		    	</div>
		 	</div>
	 	</div>

	 	<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label"><span class="custom-color-red">*</span>确认密码：</label>
		    	<div class="layui-input-block">
			     	<input type="password" name="password2" autocomplete="off" class="layui-input" lay-verify="required|password_and_password2">
		    	</div>
		 	</div>
	 	</div>

		<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label">账号类型：</label>
		    	<div class="layui-input-block">
			      	<select name="type" id="type" lay-filter="type" lay-verify="required">
	                    <option value=""></option>
			      		{volist name='usertype' id='ut'}
	                    <option value="{$ut.code}">{$ut.desc}</option>
	                    {/volist}
			      	</select>
		    	</div>
		  	</div>
	  	</div>

	  	<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label">账号关联：</label>
		    	<div class="layui-input-block">
			      	<select name="admin_relate" id="admin_relate" lay-filter="admin_relate" lay-search>
						<option value=""></option>
			      	</select>
		    	</div>
		  	</div>
	  	</div>

	  	<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label">角色：</label>
		    	<div class="layui-input-block">
			      	<select name="role">
			      		<option value=""></option>
			      	</select>
		    	</div>
		  	</div>
	  	</div>

		<div class="layui-row">
		  	<div class="layui-form-item layui-form-text layui-col-sm6">
		  	    <label class="layui-form-label">描述：</label>
		  	    <div class="layui-input-block">
		  	      	<textarea name="mark" value="" placeholder="说点什么...100个字符以内" class="layui-textarea" lay-verify="max100"></textarea>
		  	    </div>
		  	 </div>
	 	</div>

	 	<div class="layui-row">
		  	<div class="layui-form-item layui-col-sm6">
		    	<label class="layui-form-label"><span class="custom-color-red">*</span>是否开启：</label>
		    	<div class="layui-input-block">
		     		<input type="radio" name="onoff" value="1" title="开启">
 	      			<input type="radio" name="onoff" value="0" title="禁用" checked>
		    	</div>
		 	</div>
	 	</div>

		<div class="layui-row">
		  	<div class="layui-form-item">
			    <div class="layui-input-block">
			     	<button class="layui-btn custom-sm-fluid" lay-submit lay-filter="formDemo">提交</button>
			    </div>
		  	</div>
	  	</div>

	</form>

</div>
{include file="common/_footer"}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
layui.use('form', function() {

	var layer = layui.layer;
	var form  = layui.form;

	form.verify({
		password_and_password2: function(value, item) {

			if(value != $('#password').val()) {

		 	 	return '确认密码与初始密码不一致';
			}
		},

		admin_name: [/^[\S]{4,16}$/ ,'用户登录名长度为4-16位'],
		password: [/^[\S]{4,16}$/ ,'密码长度为4-16位'],
		max100: [/^.{0,100}$/ ,'描述最多输入100字符'],
	});

	//监听提交
	form.on('submit(formDemo)', function(data) {

		$.ajax({
		    type : "POST",
		    url  : "{:url('User/userHanPage')}",
		    data : $("#form-article").serialize(),
		    success : function(e){
		        if(e != 'success') {
		            layer.msg(e,{icon: 5,time:3000});
		            return false;
		        }
		        layer.msg('新增成功',{icon:1,time:1000},function(){layer.open({type: 3});parent.location.reload();});
		    },
		    error : function(e){
		        layer.msg('新增错误',{icon: 5,time:3000});
		    }
		});

		return false;
	});

	form.on('select(type)', function(data) {

		choice_type(data.value);
		choice_role(data.value);
	});

	//判断选择的用户类型
	function choice_type(msg) {

	    $.ajax({
	        type: 'GET',
	        url: 'getUserType?usertype=' + msg,
	        dataType: 'json',
	        success: function(data) {

	            if (data) {

	                var name = '';
	                var id   = '';
	                var html_data = '<option value="">不关联</option>';

	                for(var i=0;i<data.length;i++) {

	                    if (msg == 'channel') {

	                        var id     = data[i]['channel_id'];
	                        var name   = data[i]['channel_id']+'-'+data[i]['company_name'];
	                    } else if (msg == 'plat') {

	                        var id     = data[i]['plat_id'];
	                        var name   = data[i]['plat_id']+'-'+data[i]['platfrom'];
	                    }

	                    html_data += '<option value="'+id+'">'+name+'</option>';
	                }

	                $("#admin_relate").html(html_data);
	            } else {

	                $("#admin_relate").html('<option value=""></option>');
	            }

	            form.render('select');
	        },
	        error:function(data) {
	            console.log(data.msg);
	        },
	    });
	}

	//判断选择的角色
	function choice_role(msg) {

	    $.ajax({
	        type: 'GET',
	        url: 'getUserType?api=1&usertype=' + msg,
	        dataType: 'json',
	        success: function(data) {

	            var html_data = '<option value=""> </option>';
	            if (data) {
	                for(var i=0;i<data.length;i++) {

	                html_data += '<option value="'+data[i]['role_id']+'" >'+data[i]['role_name']+'</option>';
	            	}
	            }

	            $("select[name=role]").html(html_data);
	            form.render('select');
	        },
	        error:function(data) {
	            console.log(data.msg);
	        },
	    });
	}

});

</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>